﻿Imports AcadEx = Autodesk.AutoCAD.Runtime.Exception
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices

''' <summary>
''' Author Rajab Ma 
''' majianle@gmail.com
''' </summary>
''' <remarks></remarks>

Public Class CreateEnt
    ''' <summary>
    '''  将实体发送到模型空间以显示 
    ''' </summary>
    ''' <param name="ent">实体对象</param>
    ''' <returns>实体的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function PostToModelSpace(ByVal ent As Entity) _
                           As ObjectId
        Dim objId As ObjectId = ObjectId.Null
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction
            Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), _
                                              BlockTable)
            Dim model_space As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.ModelSpace), _
                                                             OpenMode.ForWrite), _
                                                             BlockTableRecord)
            Try
                objId = model_space.AppendEntity(ent)
                trans.AddNewlyCreatedDBObject(ent, True)
                trans.Commit()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Util.Output(ex.Message)
            End Try
        End Using

        Return objId

    End Function

#Region "第二章第1节 创建直线"

    ''' <summary>
    ''' 创建并显示直线
    ''' </summary>
    ''' <param name="ptStart">起点</param>
    ''' <param name="ptEnd">终点</param>
    ''' <returns>直线的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateLine(ByVal ptStart As Point3d, ByVal ptEnd As Point3d) _
                           As ObjectId
        Dim pLine As Line = New Line(ptStart, ptEnd)
        Return PostToModelSpace(pLine)

    End Function
#End Region

#Region "第二章第3节 创建圆"
    ''' <summary>
    ''' 创建并显示圆
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="vec">法线</param>
    ''' <param name="radius">半径</param>
    ''' <returns>圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateCircle(ByVal ptCenter As Point3d, _
                                        ByVal vec As Vector3d, _
                                        ByVal radius As Double) _
                           As ObjectId

        Dim _circle As Circle = New Circle(ptCenter, vec, radius)
        Return PostToModelSpace(_circle)

    End Function

    ''' <summary>
    ''' 创建并显示XOY平面上的圆
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <returns>圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateCircle(ByVal ptCenter As Point3d, _
                                        ByVal radius As Double) _
                           As ObjectId
        Return CreateCircle(ptCenter, Vector3d.ZAxis, radius)

    End Function


    ''' <summary>
    ''' 两点法创建圆
    ''' </summary>
    ''' <param name="pt1">圆上一点</param>
    ''' <param name="pt2">圆上一点</param>
    ''' <returns>圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateCircle(ByVal pt1 As Point2d, _
                                        ByVal pt2 As Point2d) _
                           As ObjectId

        Dim pt As Point2d = Calculation.MiddlePoint(pt1, pt2)
        Dim ptCenter As Point3d = Calculation.Pt2dTo3d(pt)
        Dim radius As Double = pt1.GetDistanceTo(pt2)
        Return CreateCircle(ptCenter, radius)

    End Function


    ''' <summary>
    ''' 三点法创建圆
    ''' </summary>
    ''' <param name="pt1"></param>
    ''' <param name="pt2"></param>
    ''' <param name="pt3"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CreateCircle(ByVal pt1 As Point2d, _
                                        ByVal pt2 As Point2d, _
                                        ByVal pt3 As Point2d) _
                          As ObjectId
        Dim getArc As CircularArc2d = New CircularArc2d(pt1, pt2, pt3)
        Dim ptCenter As Point3d = New Point3d(getArc.Center.X, getArc.Center.Y, 0)
        Return CreateCircle(ptCenter, getArc.Radius)

    End Function

    Public Shared Function CreateArc(ByVal ptCenter As Point3d, _
                                     ByVal vec As Vector3d, _
                                     ByVal radius As Double, _
                                     ByVal startAngle As Double, _
                                     ByVal endAngle As Double) _
            As ObjectId

        Dim _arc As Arc = New Arc(ptCenter, vec, radius, startAngle, endAngle)
        Return PostToModelSpace(_arc)

    End Function

#End Region

#Region "第二章第4节 创建圆弧"
    ''' <summary>
    ''' 创建XOY平面上的圆弧
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <param name="startAngle">起始弧度</param>
    ''' <param name="endAngle">终止弧度</param>
    ''' <returns>圆弧的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateArc(ByVal ptCenter As Point2d, _
                                     ByVal radius As Double, _
                                     ByVal startAngle As Double, _
                                     ByVal endAngle As Double) _
           As ObjectId

        Return CreateArc(Calculation.Pt2dTo3d(ptCenter), Vector3d.ZAxis, radius, startAngle, endAngle)


    End Function




    ''' <summary>
    ''' 三点法创建并显示圆弧
    ''' </summary>
    ''' <param name="ptStart">起始点</param>
    ''' <param name="ptOnArc">弧上任一点</param>
    ''' <param name="ptEnd">终止点</param>
    ''' <returns>圆弧的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateArc(ByVal ptStart As Point2d, _
                                     ByVal ptOnArc As Point2d, _
                                     ByVal ptEnd As Point2d) _
                           As ObjectId

        Dim getArc As CircularArc2d = New CircularArc2d(ptStart, ptOnArc, ptEnd)
        Dim ptCenter As Point2d = getArc.Center
        Dim radius As Double = getArc.Radius
        Dim vecStart As Vector2d = New Vector2d(ptStart.X - ptCenter.X, ptStart.Y - ptCenter.Y)
        Dim vecEnd As Vector2d = New Vector2d(ptEnd.X - ptCenter.X, ptEnd.Y - ptCenter.Y)
        Dim startAngle As Double = vecStart.Angle
        Dim endAngle As Double = vecEnd.Angle
        Return CreateArc(ptCenter, radius, startAngle, endAngle)


    End Function

    '起点、圆心、中点创建圆弧
    Public Shared Function CreateArcSCE(ByVal ptStart As Point2d, _
                                        ByVal ptCenter As Point2d, _
                                        ByVal ptEnd As Point2d) _
                           As ObjectId


        Dim radius As Double = ptCenter.GetDistanceTo(ptStart)
        Dim vecStart As Vector2d = New Vector2d(ptStart.X - ptCenter.X, ptStart.Y - ptCenter.Y)
        Dim vecEnd As Vector2d = New Vector2d(ptEnd.X - ptCenter.X, ptEnd.Y - ptCenter.Y)
        Dim startAngle As Double = vecStart.Angle
        Dim endAngle As Double = vecEnd.Angle
        Return CreateArc(ptCenter, radius, startAngle, endAngle)

    End Function

    '起点、圆心、圆弧角度创建圆弧
    Public Shared Function CreateArc(ByVal ptStart As Point2d, _
                                     ByVal ptCenter As Point2d, _
                                     ByVal angle As Double) _
                           As ObjectId


        Dim radius As Double = ptCenter.GetDistanceTo(ptStart)
        Dim vecStart As Vector2d = New Vector2d(ptStart.X - ptCenter.X, ptStart.Y - ptCenter.Y)

        Dim startAngle As Double = vecStart.Angle
        Dim endAngle As Double = startAngle + angle
        Return CreateArc(ptCenter, radius, startAngle, endAngle)

    End Function

#End Region

#Region "第二章第5节 创建多段线"

    ''' <summary>
    ''' 根据点线段创建并显示指定线宽的多线段
    ''' </summary>
    ''' <param name="points">点集合</param>
    ''' <param name="width">线宽</param>
    ''' <returns>线段的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreatePolyline(ByRef points As Point2dCollection, _
                                          ByVal width As Double) _
                           As ObjectId

        Dim numVertices As Integer = points.Count
        Dim poly As Polyline = New Polyline(numVertices)
        For i As Integer = 0 To numVertices - 1
            poly.AddVertexAt(i, points(i), 0, width, width)
        Next
        Dim polyId As ObjectId = PostToModelSpace(poly)
        Return polyId

    End Function

    ''' <summary>
    ''' 根据点线段创建并显示线宽为0的多线段
    ''' </summary>
    ''' <param name="points">点集合</param>
    ''' <returns>线段的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreatePolyline(ByRef points As Point2dCollection) _
                           As ObjectId

        Return CreatePolyline(points, 0)

    End Function

    ''' <summary>
    ''' 根据起点和终点创建并显示指定线宽的线段
    ''' </summary>
    ''' <param name="ptStart">起始点</param>
    ''' <param name="ptEnd">终止点</param>
    ''' <param name="width">线宽</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CreatePolyline(ByVal ptStart As Point2d, _
                                          ByVal ptEnd As Point2d, _
                                          ByVal width As Double) _
                           As ObjectId

        Dim points As Point2dCollection = New Point2dCollection()
        points.Add(ptStart)
        points.Add(ptEnd)
        Return CreatePolyline(points, width)

    End Function

    ''' <summary>
    ''' 根据点集合创建并显示三维多线段
    ''' </summary>
    ''' <param name="points">点集合</param>
    ''' <returns>线段的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function Create3dPolyline(ByRef points As Point3dCollection) _
                           As ObjectId

        Dim poly3d As Polyline3d = New Polyline3d(Poly3dType.SimplePoly, points, False)
        Return PostToModelSpace(poly3d)

    End Function

    ''' <summary>
    ''' 创建并显示正多边形
    ''' </summary>
    ''' <param name="ptCenter">中心点</param>
    ''' <param name="number">边的数目</param>
    ''' <param name="radius">外接圆半径</param>
    ''' <param name="rotation">旋转弧度</param>
    ''' <param name="width">线宽</param>
    ''' <returns>多边形的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreatePolygon(ByVal ptCenter As Point2d, _
                                         ByVal number As Integer, _
                                         ByVal radius As Double, _
                                         ByVal rotation As Double, _
                                         ByVal width As Double) _
                            As ObjectId

        Dim points As Point2dCollection = New Point2dCollection()
        Dim angle As Double = 2 * Math.PI / number
        Dim pt As Point2d
        For i As Integer = 0 To number - 1
            pt = New Point2d(ptCenter.X + radius * Math.Cos(i * angle), _
                             ptCenter.Y + radius * Math.Sin(i * angle))
            points.Add(pt)
            pt = Nothing
        Next

        Dim polyId As ObjectId = CreatePolyline(points, width)

        '将其闭合
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction
            Try
                Dim poly As Polyline = DirectCast(trans.GetObject(polyId, OpenMode.ForWrite), _
                                                  Polyline)
                poly.Closed = True

            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Util.Output(ex.Message)
            Finally
                trans.Commit()
            End Try
        End Using

        EntityModify.Rotate(polyId, ptCenter, rotation)
        Return polyId

    End Function

    ''' <summary>
    ''' 创建并显示矩形
    ''' </summary>
    ''' <param name="pt1">左上角点</param>
    ''' <param name="pt2">右下角点</param>
    ''' <param name="width">线宽</param>
    ''' <returns>矩形的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateRectangle(ByVal pt1 As Point2d, _
                                           ByVal pt2 As Point2d, _
                                           ByVal width As Double) _
                           As ObjectId

        Dim x1 As Double = pt1.X
        Dim x2 As Double = pt2.X
        Dim y1 As Double = pt1.Y
        Dim y2 As Double = pt2.Y


        Dim ptLeftBottom As Point2d = New Point2d(Math.Min(x1, x2), Math.Min(y1, y2))
        Dim ptRightBottom As Point2d = New Point2d(Math.Max(x1, x2), Math.Min(y1, y2))
        Dim ptRightTop As Point2d = New Point2d(Math.Max(x1, x2), Math.Max(y1, y2))
        Dim ptLeftTop As Point2d = New Point2d(Math.Min(x1, x2), Math.Max(y1, y2))

        Dim poly As Polyline = New Polyline(4)
        poly.AddVertexAt(0, ptLeftBottom, 0, width, width)
        poly.AddVertexAt(1, ptRightBottom, 0, width, width)
        poly.AddVertexAt(2, ptRightTop, 0, width, width)
        poly.AddVertexAt(3, ptLeftTop, 0, width, width)

        poly.Closed = True
        Dim polyId As ObjectId = PostToModelSpace(poly)
        Return polyId

    End Function

    ''' <summary>
    ''' 创建并显示圆
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <param name="width">线宽</param>
    ''' <returns>圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreatePolyCircle(ByVal ptCenter As Point2d, _
                                            ByVal radius As Double, _
                                            ByVal width As Double) _
                           As ObjectId

        Dim pt1, pt2, pt3 As Point2d
        pt1 = New Point2d(ptCenter.X + radius, ptCenter.Y)
        pt2 = New Point2d(ptCenter.X - radius, ptCenter.Y)
        pt3 = New Point2d(ptCenter.X + radius, ptCenter.Y)
        Dim poly As Polyline = New Polyline(3)
        poly.AddVertexAt(0, pt1, 1, width, width)
        poly.AddVertexAt(1, pt2, 1, width, width)
        poly.AddVertexAt(2, pt3, 1, width, width)
        poly.Closed = True

        Dim polyId As ObjectId = PostToModelSpace(poly)
        Return polyId

    End Function

    ''' <summary>
    ''' 创建并显示圆弧
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <param name="angleStart">起始角(弧)度</param>
    ''' <param name="angleEnd">终止角(弧)度</param>
    ''' <param name="width">线宽</param>
    ''' <returns>圆弧的对象Id</returns>
    ''' <remarks></remarks>

    Public Shared Function CreatePolyArc(ByVal ptCenter As Point2d, _
                                         ByVal radius As Double, _
                                         ByVal angleStart As Double, _
                                         ByVal angleEnd As Double, _
                                         ByVal width As Double) _
                           As ObjectId

        Dim pt1, pt2 As Point2d
        pt1 = New Point2d(ptCenter.X + radius * Math.Cos(angleStart), _
                          ptCenter.Y + radius * Math.Sin(angleStart))
        pt1 = New Point2d(ptCenter.X + radius * Math.Cos(angleEnd), _
                          ptCenter.Y + radius * Math.Sin(angleEnd))
        Dim poly As Polyline = New Polyline(3)
        poly.AddVertexAt(0, pt1, Math.Tan((angleEnd - angleStart) / 4), width, width)
        poly.AddVertexAt(1, pt2, 0, width, width)


        Dim polyId As ObjectId = PostToModelSpace(poly)
        Return polyId

    End Function

    ''' <summary>
    ''' 创建并显示椭圆
    ''' </summary>
    ''' <param name="ptCenter">椭圆中心</param>
    ''' <param name="vecNormal">法线</param>
    ''' <param name="majorAxis">主轴(1/2长轴矢量)</param>
    ''' <param name="ratio"></param>
    ''' <returns>椭圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateEllipse(ByVal ptCenter As Point3d, _
                                         ByVal vecNormal As Vector3d, _
                                         ByVal majorAxis As Vector3d, _
                                         ByVal ratio As Double) _
                           As ObjectId

        Dim _ellipse As Ellipse = New Ellipse(ptCenter, vecNormal, majorAxis, ratio, 0.0, 6.2831853071)
        Return PostToModelSpace(_ellipse)

    End Function

#End Region

#Region "第二章第6节 创建椭圆"

    ''' <summary>
    ''' 根据两点(外接矩形)创建并显示椭圆
    ''' </summary>
    ''' <param name="pt1">左上角点</param>
    ''' <param name="pt2">右下角点</param>
    ''' <returns>椭圆的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateEllipse(ByVal pt1 As Point2d, _
                                         ByVal pt2 As Point2d) _
                              As ObjectId


        Dim ptCenter As Point3d = Calculation.MiddlePoint(Calculation.Pt2dTo3d(pt1), _
                                                          Calculation.Pt2dTo3d(pt2))
        Dim majorAxis As Vector3d = New Vector3d(Math.Abs(pt1.X - pt2.X) / 2, 0, 0)
        Dim ratio As Double = Math.Abs((pt1.Y - pt2.Y) / (pt1.X - pt2.X))
        Return CreateEllipse(ptCenter, Vector3d.ZAxis, majorAxis, ratio)

    End Function

    ''' <summary>
    '''  创建并显示样条曲线
    ''' </summary>
    ''' <param name="points">样条曲线拟合点</param>
    ''' <param name="order">拟合曲线阶数</param>
    ''' <param name="fitTolerance">允许的拟合误差</param>
    ''' <returns>样条曲线的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateSpline(ByRef points As Point3dCollection, _
                                        Optional ByVal order As Integer = 4, _
                                        Optional ByVal fitTolerance As Double = 0.0) _
                           As ObjectId
        Debug.Assert(order >= 2 And order <= 26)
        Dim _spline As Spline = New Spline(points, order, fitTolerance)
        Return PostToModelSpace(_spline)

    End Function


    ''' <summary>
    '''  创建并显示样条曲线
    ''' </summary>
    ''' <param name="points">样条曲线拟合点</param>
    ''' <param name="startTangent">样条曲线起点切线方向</param>
    ''' <param name="endTangent">样条曲线终点切线方向</param>
    ''' <param name="order">拟合曲线阶数</param>
    ''' <param name="fitTolerance">允许的拟合误差</param>
    ''' <returns>样条曲线的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateSpline(ByRef points As Point3dCollection, _
                                        ByVal startTangent As Vector3d, _
                                        ByVal endTangent As Vector3d, _
                                        Optional ByVal order As Integer = 4, _
                                        Optional ByVal fitTolerance As Double = 0.0) _
                           As ObjectId
        Debug.Assert(order >= 2 And order <= 26)
        Dim _spline As Spline = New Spline(points, startTangent, endTangent, order, fitTolerance)
        Return PostToModelSpace(_spline)

    End Function






#End Region

#Region "第二章第7节 创建面域"

    Public Shared Function CreateRegion(ByRef curveIds As ObjectIdCollection) _
                           As ObjectIdCollection
        Dim regionIds As ObjectIdCollection = New ObjectIdCollection()
        Dim curves As DBObjectCollection = New DBObjectCollection()
        Dim regions As DBObjectCollection
        Dim ent As Entity = Nothing
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                '用curveIds初始化curves
                For Each id As ObjectId In curveIds
                    ent = DirectCast(trans.GetObject(id, OpenMode.ForRead), _
                                     Entity)
                    If TypeOf ent Is Curve Then
                        curves.Add(ent)
                    End If
                Next
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                Util.Output(ex.Message)
            End Try

            regions = Region.CreateFromCurves(curves)
            If regions <> Nothing Then
                For Each obj As DBObject In regions
                    Dim region As Region = DirectCast(obj, Region)
                    region.SetDatabaseDefaults()
                    Dim regionId As ObjectId
                    regionId = PostToModelSpace(region)
                    regionIds.Add(regionId)
                Next
            Else
                For i As Integer = 0 To regions.Count - 1
                    regions(i) = Nothing
                Next
                regions.Clear()
            End If
        End Using

        Return regionIds
    End Function

#End Region

#Region "第二章第8节 创建文字"
    ''' <summary>
    ''' 创建并显示多行文字
    ''' </summary>
    ''' <param name="ptInsert">插入位置</param>
    ''' <param name="text">文字内容</param>
    ''' <param name="style">文字样式</param>
    ''' <param name="height">文字高度</param>
    ''' <param name="width">文字宽度</param>
    ''' <returns>多行文字的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateText(ByVal ptInsert As Point3d, _
                                      ByVal text As String, _
                                      ByVal style As ObjectId, _
                                      Optional ByVal height As Double = 2.5, _
                                      Optional ByVal width As Double = 10.0) _
                           As ObjectId

        Dim _mtext As MText = New MText()
        With _mtext
            .Contents = text
            .Location = ptInsert
            .TextStyle = style
            .TextHeight = height
            .Width = width
        End With

        Return PostToModelSpace(_mtext)

    End Function

#End Region

#Region "第二章第9节 创建填充"
    ''' <summary>
    ''' 根据一组首尾连接的对象创建填充
    ''' </summary>
    ''' <param name="objIds">组成填充边界的对象ID </param>
    ''' <param name="patName">填充图案的名称</param>
    ''' <param name="bAssociative">填充的关联性</param>
    ''' <returns>填充的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateHatch(ByRef objIds As ObjectIdCollection, _
                                       ByVal patName As String, _
                                       ByVal bAssociative As Boolean) _
                           As ObjectId

        Dim _hatch As Hatch = New Hatch()
        With _hatch
            '设置填充平面
            .Normal = Vector3d.ZAxis
            .Elevation = 0
            '设置关联性
            .Associative = bAssociative
            '填充图案
            .PlotStyleName = patName
            '添加填充边界
            .AppendLoop(HatchLoopTypes.External, objIds)
            '显示填充对象
            .EvaluateHatch(True)
        End With
        '添加到模型空间
        Dim hatchId As ObjectId = PostToModelSpace(_hatch)

        '如果是关联性的填充，将填充对象与边界绑定，以便使其能获得边界对象修改的通知 
        If bAssociative Then
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Using trans As Transaction = db.TransactionManager.StartTransaction()
                Try
                    For Each objId As ObjectId In objIds
                        Dim ent As Entity = DirectCast(trans.GetObject(objId, OpenMode.ForWrite), _
                                                       Entity)
                        '添加一个永久反应器 
                        ent.HasPersistentReactor(objId)
                    Next
                Catch ex As AcadEx
                    Util.Output(ex.Message)
                Finally
                    trans.Commit()
                End Try
            End Using
        End If

    End Function

#End Region

#Region "第二章第10节 创建尺寸标注"
    ''' <summary>
    ''' 创建对齐标注
    ''' </summary>
    ''' <param name="pt1">第一条尺寸边界线的起点</param>
    ''' <param name="pt2">第二条尺寸边界线的起点</param>
    ''' <param name="ptLine">通过尺寸线的一点</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>对齐标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimAligned(ByVal pt1 As Point3d, _
                                            ByVal pt2 As Point3d, _
                                            ByVal ptLine As Point3d, _
                                            ByVal dimText As String, _
                                            ByVal dimStyle As ObjectId) _
                            As ObjectId

        Dim _dim As AlignedDimension = New AlignedDimension(pt1, pt2, ptLine, dimText, dimStyle)
        Return PostToModelSpace(_dim)

    End Function

    ''' <summary>
    ''' 创建对齐标注
    ''' </summary>
    ''' <param name="pt1">第一条尺寸边界线的起点</param>
    ''' <param name="pt2">第二条尺寸边界线的起点</param>
    ''' <param name="ptLine">通过尺寸线的一点</param>
    ''' <param name="vecOffset">标注文字位置的偏移量</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>对齐标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimAligned(ByVal pt1 As Point3d, _
                                            ByVal pt2 As Point3d, _
                                            ByVal ptLine As Point3d, _
                                            ByVal vecOffset As Vector3d, _
                                            ByVal dimText As String, _
                                            ByVal dimStyle As ObjectId) _
                            As ObjectId

        Dim _dim As AlignedDimension = New AlignedDimension(pt1, pt2, ptLine, dimText, dimStyle)
        Dim dimensionId As ObjectId = PostToModelSpace(_dim)

        '注意：移动标注文字必须在将其添加到模型空间之后进行
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Try
                '打开已经创建的标注，对文字的位置进行修改 
                Dim getDim As AlignedDimension = DirectCast(trans.GetObject(dimensionId, OpenMode.ForWrite), _
                                                            AlignedDimension)
                ' 移动文字位置前，需先指定尺寸线的变化情况（这里指定为：尺寸线不动，在文字和尺寸线之间加箭头）
                getDim.Dimtmove = 1
                Dim pText As Point3d = getDim.TextPosition
                pText = pText + vecOffset
                getDim.TextPosition = pText
            Catch ex As AcadEx
                Util.Output(ex.Message)
            Finally
                trans.Commit()
            End Try
        End Using

        Return dimensionId

    End Function

    ''' <summary>
    ''' 创建转角标注
    ''' </summary>
    ''' <param name="rotation">标注的旋转角度</param>
    ''' <param name="pt1">第一条尺寸边界线的起点</param>
    ''' <param name="pt2">第二条尺寸边界线的起点</param>
    ''' <param name="ptLine">通过尺寸线的一点</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>对齐标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimRotated(ByVal rotation As Double, _
                                            ByVal pt1 As Point3d, _
                                            ByVal pt2 As Point3d, _
                                            ByVal ptLine As Point3d, _
                                            ByVal dimText As String, _
                                            ByVal dimStyle As ObjectId) _
                            As ObjectId
        Dim _dim As RotatedDimension = New RotatedDimension(rotation, pt1, pt2, ptLine, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    '''  创建半径标注
    ''' </summary>
    ''' <param name="ptCenter">标注曲线的中心点</param>
    ''' <param name="ptChord">引线附着的坐标</param>
    ''' <param name="leaderLength">引线长度</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>半径标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimRadial(ByVal ptCenter As Point3d, _
                                           ByVal ptChord As Point3d, _
                                           ByVal leaderLength As Double, _
                                           ByVal dimText As String, _
                                           ByVal dimStyle As ObjectId) _
                            As ObjectId
        Dim _dim As RadialDimension = New RadialDimension(ptCenter, ptChord, leaderLength, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 创建半径标注
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <param name="angle">标注尺寸线的旋转角度</param>
    ''' <param name="leaderLength">引线长度</param>
    ''' <returns>半径标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimRadial(ByVal ptCenter As Point3d, _
                                           ByVal radius As Double, _
                                           ByVal angle As Double, _
                                           Optional ByVal leaderLength As Double = 5) _
                            As ObjectId

        Dim ptChord As Point3d = Calculation.PolarPoint(ptCenter, angle, radius)
        Dim _dim As RadialDimension = New RadialDimension(ptCenter, ptChord, leaderLength, Nothing, ObjectId.Null)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 创建直径标注
    ''' </summary>
    ''' <param name="ptChord1">直径第1个端点</param>
    ''' <param name="ptChord2">直径第2个端点</param>
    ''' <param name="leaderLength">引线长度</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>直径标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimDiametric(ByVal ptChord1 As Point3d, _
                                              ByVal ptChord2 As Point3d, _
                                              ByVal leaderLength As Double, _
                                              ByVal dimText As String, _
                                              ByVal dimStyle As ObjectId) _
                            As ObjectId

        Dim _dim As DiametricDimension = New DiametricDimension(ptChord1, ptChord2, leaderLength, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function


    ''' <summary>
    ''' 创建直径标注
    ''' </summary>
    ''' <param name="ptCenter">圆心</param>
    ''' <param name="radius">半径</param>
    ''' <param name="angle">标注尺寸线的旋转角度</param>
    ''' <param name="leaderLength">引线长度</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>半径标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimDiametric(ByVal ptCenter As Point3d, _
                                              ByVal radius As Double, _
                                              ByVal angle As Double, _
                                              ByVal leaderLength As Double, _
                                              ByVal dimText As String, _
                                              ByVal dimStyle As ObjectId) _
                           As ObjectId

        Dim ptChord1 As Point3d = Calculation.PolarPoint(ptCenter, angle, radius)
        Dim ptChord2 As Point3d = Calculation.PolarPoint(ptCenter, angle + Math.PI, radius)
        Dim _dim As DiametricDimension = New DiametricDimension(ptChord1, ptChord2, leaderLength, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 根据两条直线的关系来创建角度标注
    ''' </summary>
    ''' <param name="ptStart1">第1条直线起点</param>
    ''' <param name="ptEnd1">第1条直线终点</param>
    ''' <param name="ptStart2">第2条直线起点</param>
    ''' <param name="ptEnd2">第2条直线终点</param>
    ''' <param name="ptArc">标注尺寸线通过点</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDim2LineAngular(ByVal ptStart1 As Point3d, _
                                                 ByVal ptEnd1 As Point3d, _
                                                 ByVal ptStart2 As Point3d, _
                                                 ByVal ptEnd2 As Point3d, _
                                                 ByVal ptArc As Point3d, _
                                                 ByVal dimText As String, _
                                                 ByVal dimStyle As ObjectId) _
                           As ObjectId

        Dim _dim As LineAngularDimension2 = New LineAngularDimension2(ptStart1, _
                                            ptEnd1, ptStart2, ptEnd2, ptArc, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 根据顶点、起始点、终止点和标注尺寸线通过点来创建角度标注：
    ''' </summary>
    ''' <param name="ptCenter">顶点</param>
    ''' <param name="ptEnd1">起始点</param>
    ''' <param name="ptEnd2">终止点</param>
    ''' <param name="ptArc">标注尺寸线通过点</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDim3PtAngular(ByVal ptCenter As Point3d, _
                                                 ByVal ptEnd1 As Point3d, _
                                                 ByVal ptEnd2 As Point3d, _
                                                 ByVal ptArc As Point3d, _
                                                 ByVal dimText As String, _
                                                 ByVal dimStyle As ObjectId) _
                          As ObjectId

        Dim _dim As Point3AngularDimension = New Point3AngularDimension(ptCenter, _
                                             ptEnd1, ptEnd2, ptArc, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 创建坐标标注
    ''' </summary>
    ''' <param name="useXAxis">是否是 X 轴标注</param>
    ''' <param name="ptStart">标注箭头的起始位</param>
    ''' <param name="ptEnd">标注箭头的终止位置</param>
    ''' <param name="dimText">标注文字</param>
    ''' <param name="dimStyle">标注样式</param>
    ''' <returns>标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimOrdinate(ByVal useXAxis As Boolean, _
                                             ByVal ptStart As Point3d, _
                                             ByVal ptEnd As Point3d, _
                                             ByVal dimText As String, _
                                             ByVal dimStyle As ObjectId) _
                           As ObjectId

        Dim _dim As OrdinateDimension = New OrdinateDimension(useXAxis, ptStart, ptEnd, dimText, dimStyle)
        Return PostToModelSpace(_dim)
    End Function

    ''' <summary>
    ''' 创建一个点的 X、Y坐标标注
    ''' </summary>
    ''' <param name="ptDef">点坐标</param>
    ''' <param name="ptTextX">X点</param>
    ''' <param name="ptTextY">Y点</param>
    ''' <returns>标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimOrdinate(ByVal ptDef As Point3d, _
                                             ByVal ptTextX As Point3d, _
                                             ByVal ptTextY As Point3d) _
                           As ObjectIdCollection

        Dim _dimId As ObjectId = ObjectId.Null
        Dim _dimIDs As ObjectIdCollection = New ObjectIdCollection()
        _dimId = CreateDimOrdinate(True, ptDef, ptTextX, Nothing, ObjectId.Null)
        _dimIDs.Add(_dimId)
        _dimId = CreateDimOrdinate(False, ptDef, ptTextY, Nothing, ObjectId.Null)
        _dimIDs.Add(_dimId)
        Return _dimIDs
    End Function

    ''' <summary>
    ''' 创建一个点的 X、Y坐标标注
    ''' </summary>
    ''' <param name="ptDef">点坐标</param>
    ''' <param name="vecOffsetX">X方向偏移位置</param>
    ''' <param name="vecOffsetY">Y方向偏移位置</param>
    ''' <returns>标注的对象Id</returns>
    ''' <remarks></remarks>
    Public Shared Function CreateDimOrdinate(ByVal ptDef As Point3d, _
                                             ByVal vecOffsetX As Vector3d, _
                                             ByVal vecOffsetY As Vector3d) _
                           As ObjectIdCollection

        Dim ptTextX, ptTextY As Point3d
        ptTextX = ptDef + vecOffsetX
        ptTextY = ptDef + vecOffsetY
        Return CreateDimOrdinate(ptDef, ptTextX, ptTextY)
    End Function
#End Region



End Class
